﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class _Default : Page
    {
        private DataTable gvDataTable = new DataTable();

        private DataTable DateOther
        {
            get
            {
                if (gvDataTable.Rows.Count > 0)
                {
                    return gvDataTable;
                }
                else
                {
                    var sql = "select * from Articles";
                    var dt = DbHelper.GetDataTable(sql);
                    gvDataTable = dt;
                    return dt;
                }

            }
        }







     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            
            GridView1.DataSource = this.DateOther;
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValues(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where Id={0}", id);
            DbHelper.AddOrUpdateOrDelete(sql);
            GridView1.EditIndex = -1;
            FillData();


        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValues(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);
            var title = GetValues(e.RowIndex, 1);
            var content = GetValues(e.RowIndex, 2);
            var author = GetValues(e.RowIndex, 3);
            var remarks = GetValues(e.RowIndex, 4);

            if (isOk)
            {
                var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id={4}", title, content,author, remarks, id);
                DbHelper.AddOrUpdateOrDelete(sql);
            }
            else
            {
                var sql = string.Format("insert into Articles(Title,Content,Author,Remarks) values('{0}','{1}','{2}',{3})", title, content,author, remarks);
                DbHelper.AddOrUpdateOrDelete(sql);
            }

            GridView1.EditIndex = -1;
            FillData();
        }

        private string GetValues(int rowIndex, int v)
        {
            var cell = GridView1.Rows[rowIndex].Cells[v];
            var id = cell.Controls.Count > 0 ? ((TextBox)cell.Controls[0]).Text : cell.Text;
            return id;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = this.DateOther.Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}